package com.wwfish.cmsui.modules.content.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ContextMenuEvent;
import com.google.gwt.event.dom.client.ContextMenuHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.*;
import com.nexustar.gwt.widgets.client.asyn.FishAsyncCallback;
import com.nexustar.gwt.widgets.client.asyn.IAsyncModelCallback;
import com.nexustar.gwt.widgets.client.ui.iprovider.ITreeProvider;
import com.nexustar.gwt.widgets.client.ui.panel.ContentPanel;
import com.nexustar.gwt.widgets.client.ui.toolbar.MyMenuBar;
import com.nexustar.gwt.widgets.client.ui.toolbar.MyMenuItem;
import com.nexustar.gwt.widgets.client.ui.tree.ViewTree;
import com.nexustar.gwt.widgets.client.ui.window.IMessageConfirmCall;
import com.nexustar.gwt.widgets.client.ui.window.Message;
import com.wwfish.cms.model.BaseContentDto;
import com.wwfish.cms.model.ChannelDto;
import com.wwfish.cms.model.NewsDto;
import com.wwfish.cms.service.ChannelManager;
import com.wwfish.cmsui.modules.channel.client.ChannelWidget;
import com.wwfish.cmsui.modules.channel.client.SequenceTreeWidget;
import com.wwfish.cmsui.modules.common.client.BaseCMSPage;
import com.wwfish.cmsui.modules.common.client.CMSRunAsyncCallBack;
import com.wwfish.cmsui.modules.common.client.util.ServiceFactory;
import com.wwfish.gwt.reflection.client.ReflectionHelper;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2010-8-10
 * Time: 14:14:12
 * To change this template use File | Settings | File Templates.
 */
public class ContentPage extends BaseCMSPage {
    protected ContentWidget contentWidget;
    protected SimplePanel mainPanel;

    protected ChannelTree channel;


    @Override
    protected boolean isRunAsync() {
        return true;
    }

    @Override
    public void initWidgets(final DockPanel dockPanel) {
        GWT.runAsync(new CMSRunAsyncCallBack() {
            public void onSuccess() {
                dockPanel.setWidth("100%");
                channel = new ChannelTree();
                contentWidget = new ContentWidget();

                mainPanel = new SimplePanel();
                mainPanel.add(contentWidget);
                dockPanel.add(channel, DockPanel.NORTH);
                dockPanel.add(mainPanel, DockPanel.CENTER);
                addTreeEvent();

                ContentPage.this.client.success(null);

            }
        });
    }


    private void addTreeEvent() {
        channel.getTree().addSelectionHandler(new SelectionHandler<TreeItem>() {
            public void onSelection(SelectionEvent<TreeItem> treeItemSelectionEvent) {
                ChannelDto dto = (ChannelDto) treeItemSelectionEvent.getSelectedItem().getUserObject();
                StringBuffer sb = new StringBuffer();
                channel.getAllChannelName(sb, treeItemSelectionEvent.getSelectedItem());
                sb.append("[内容详情]");
                contentWidget.setContentTitle(sb.toString());
                ((BaseContentDto) contentWidget.getModelManger().getModel()).setChannelId(dto.getId());
            }
        });
    }


    @Override
    protected void ddOutPageModel(Object o) {
        channel.getTree().getModelManger().renderAsyncModel(null);
        contentWidget.getModelManger().renderModel(o);
    }


}

